package edu.bw.web.dept;

import cn.hutool.json.JSONUtil;
import edu.bw.common.CommonResult;
import edu.bw.dto.SelectDeptByConditionForm;
import edu.bw.service.DeptService;
import edu.bw.service.impl.DeptServiceImpl;

import edu.bw.utils.PageUtils;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

import java.io.IOException;

@WebServlet("/dept/selectDeptByCondition")
public class SelectDeptByConditionServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        DeptService deptService=new DeptServiceImpl();
        String reqjson = request.getReader().readLine();
        SelectDeptByConditionForm selectCondition  = JSONUtil.toBean(reqjson, SelectDeptByConditionForm.class);

        //2.调用service传递dto数据
        PageUtils pageUtils = deptService.selectDeptByCondition(selectCondition);

        //3.以JSON形式响应给前端
       response.getWriter().write(JSONUtil.toJsonStr(CommonResult.ok().put("page",pageUtils)));
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
